package com.dj.shop.controller;

import com.dj.shop.model.Brand;
import com.dj.shop.model.Manu;
import com.dj.shop.service.ManuService;
import com.dj.shop.util.BeanUtil;
import com.dj.shop.valid.Insert;
import com.dj.shop.vo.BrandVo;
import com.dj.shop.vo.ManuVo;
import com.dj.shop.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author renshp
 * @Title: ManuController
 * @Description: TODO
 * @date 2019/3/1
 */
@Controller
@Validated
@RequestMapping("/Manu")
public class ManuController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(ManuController.class);

    @Autowired
    private ManuService manuService;

    @RequestMapping(value = "/addManu", method = RequestMethod.POST)
    public @ResponseBody Result  addManu(@Validated({Insert.class}) ManuVo manuVo, BindingResult bindingResult){

        // 视图模型与持久层模型转换
        Manu manu = new Manu();
        BeanUtils.copyProperties(manuVo, manu);

        if (manuService.addManu(manu) < 1){
            return Result.error(9, "新增厂商失败");
        }

        return Result.success();
    }

    /***
     * 根据厂商id查询厂商信息
     * @return
     */
    @RequestMapping(value = "/queryManuById", method = RequestMethod.GET)
    public @ResponseBody Result queryManuById(int manuId){

        Manu manu = manuService.queryManuById(manuId);
        if (manu == null){
            return Result.error(9, "无厂商信息");
        }

        return Result.success(manu);
    }

    /***
     * 更新厂商信息
     * @param manu
     * @return
     */
    @RequestMapping(value = "/updateManu", method = RequestMethod.POST)
    public @ResponseBody Result updateManu(Manu manu){

        if (manuService.updateManu(manu) < 1){
            return Result.error(9, "更新失败");
        }

        return Result.success();
    }

    /***
     * 更新厂商状态
     * @param manuId 厂商id
     * @param manuStatus 厂商新状态
     * @return
     */
    @RequestMapping(value = "/updateManuStatus", method = RequestMethod.POST)
    public @ResponseBody Result updateManuStatus(int manuId, String manuStatus){

        if (manuService.updateManuStatus(manuId, manuStatus) < 1){
            return Result.error(9, "更新失败");
        }

        return Result.success();
    }

    /***
     * 新增品牌信息
     * @param brandVo
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/addBrand", method = RequestMethod.POST)
    public @ResponseBody Result addBrand(@Validated({Insert.class}) BrandVo brandVo, BindingResult bindingResult){

        Brand brand = new Brand();
        BeanUtils.copyProperties(brandVo, brand);

        if (manuService.addBrand(brand) < 0){
            return Result.error(9, "新增品牌失败");
        }

        return Result.success();
    }

    /***
     * 根据品牌id查询品牌信息
     * @return
     */
    @RequestMapping(value = "/queryBrandById", method = RequestMethod.GET)
    public @ResponseBody Result queryBrandById(int brandId){

        Brand Brand = manuService.queryBrandById(brandId);
        if (Brand == null){
            return Result.error(9, "无品牌信息");
        }

        return Result.success(Brand);
    }


    /***
     * 根据厂商id查询该厂商下所有品牌信息
     * @param manuId
     * @return
     */
    @RequestMapping(value = "/queryBrandsByManuId", method = RequestMethod.GET)
    public @ResponseBody Result queryBrandsByManuId(int manuId){

        System.out.println("manuId:[" + manuId + "]");

        List<Brand> brandList = manuService.queryBrandsByManuId(manuId);
        return Result.success(brandList);

    }

}
